/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package SharedDrawing;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JPanel;

import primitives.Primitive;

import common.XmlConstants;

public abstract class FourCornersShape extends BoundingBox 
{
	protected Coord m_location;
	protected Coord m_size;
	
	protected FourCornersShape()
	{
		m_composingElements = new ArrayList<Primitive>();
		
		m_location = new Coord();
		m_size = new Coord();
		
		m_resizable = true;
		
		m_cornersBox = new Rectangle2D[4];
	}
	
	@Override
	public void initShape(JPanel container, int x, int y, int width, boolean fill, Color color, Color border)
	{
		m_container = container;
		setElementsId();
		
		m_location.x = x;
		m_location.y = y;
		m_size.x = 0;
		m_size.y = 0;
		
		for(Primitive element : m_composingElements)
		{
			element.initPrimitive(x, y, width, fill, color, border);
			element.setShape(x, y, x, y);
		}	

		for (int i = 0; i < 4; i++) 
		{
			m_cornersBox[i] = new Rectangle2D.Float(x, y, 5, 5);
		}
	}	

	@Override
    protected void setElementsId()
    {
		int i = 0;
		
		for(Primitive element : m_composingElements)
		{
			element.setElementId(i);
			i++;
		}
    }
	
	@Override
    public void drawComponent(Graphics2D g2)
    {
		for(Primitive element : m_composingElements)
		{
			element.drawComponent(g2);
		}
    }
	
	@Override
	public boolean isOverObject(int x, int y)
	{
		for(Primitive element : m_composingElements)
		{
            if(element.isOverObject(x, y))
                return true;
		}
		return false;
	}
	
	public void drawObject(int x, int y) 
	{
		if(x - m_location.x >= 0)
			m_size.x = x - m_location.x;
		
		if(y - m_location.y >= 0)
			m_size.y = y - m_location.y;
		
		if(m_size.x <= 0) 
			m_size.x = 3;
		if(m_size.y <= 0)
			m_size.y = 3;
	}

	public void moveObject(int x, int y, Coord[] clickLocation) 
	{
		m_location.x = x - clickLocation[0].x;
		m_location.y = y - clickLocation[0].y;
		
		if(m_location.x < 0) 
			m_location.x = 0;
		if(m_location.y < 0)
			m_location.y = 0;
	}

	public void setClickLocation(int x, int y, Coord[] clickLocation) 
	{
		clickLocation[0].x = x - m_location.x;
		clickLocation[0].y = y - m_location.y;
	}

	public void resizeManager(int x, int y, int cornerType) {
		int sizeX, sizeY;

		switch (cornerType) {
		case 1:
			sizeX = m_size.x + m_location.x - x;
			sizeY = m_size.y + m_location.y - y;

			if (sizeX > 20) {
				m_size.x = sizeX;
				m_location.x = x;

			}
			if (sizeY > 20) {
				m_size.y = sizeY;
				m_location.y = y;
			}
			break;

		case 2:
			sizeX = m_size.x + m_location.x - x;
			sizeY = y - m_location.y;

			if (sizeX > 20) {
				m_size.x = sizeX;
				m_location.x = x;
			}
			if (sizeY > 20) {
				m_size.y = sizeY;
			}
			break;

		case 3:
			sizeX = x - m_location.x;
			sizeY = m_size.y + m_location.y - y;

			if (sizeX > 20) {
				m_size.x = sizeX;
			}
			if (sizeY > 20) {
				m_size.y = sizeY;
				m_location.y = y;
			}
			break;

		case 4:
			sizeX = x - m_location.x;
			sizeY = y - m_location.y;

			if (sizeX > 20) {
				m_size.x = sizeX;
			}
			if (sizeY > 20) {
				m_size.y = sizeY;
			}
			break;

		default:
		}
	}

	public void drawCorners(Graphics2D g) 
	{
		g.setStroke(new BasicStroke());
		
		for (int i = 0; i < 4; i++) 
		{
			g.setColor(Color.cyan);
			g.fill(m_cornersBox[i]);
			g.setColor(Color.black);
			g.draw(m_cornersBox[i]);
		}
	}
	
	public int overCorner(double x, double y) 
	{
		for (int i = 0; i < 4; i++)
		{
			if (m_cornersBox[i].contains(x, y)) 
			{
				return i + 1;
			}
		}
		return 0;
	}
	
	public void setCorners()
	{	
		m_cornersBox[0].setFrame(m_location.x, m_location.y, 5, 5);
		m_cornersBox[1].setFrame(m_location.x, m_location.y + m_size.y - 5, 5, 5);
		m_cornersBox[2].setFrame(m_location.x + m_size.x - 5, m_location.y, 5, 5);
		m_cornersBox[3].setFrame(m_location.x + m_size.x - 5, m_location.y + m_size.y - 5, 5, 5);
	}
        
	@Override   
    protected ShapeContent toShapeContent()
    {
    	ShapeContent shape = new ShapeContent();
       
        WgContent wg = new WgContent(this.getClass().getSimpleName());
        
        wg.addContent(XmlConstants.shapeContentX, String.valueOf(m_location.x));
        wg.addContent(XmlConstants.shapeContentY, String.valueOf(m_location.y));
        wg.addContent(XmlConstants.shapeContentSizeX, String.valueOf(m_size.x));
        wg.addContent(XmlConstants.shapeContentSizeY, String.valueOf(m_size.y));

        shape.setBoundingBox(wg);
        
        for(Primitive element : m_composingElements)
		{
        	shape.addElement(element.toWgContent());
		}
        
        return shape;
    }
    
	@Override
    protected void fromShapeContent(ShapeContent content)
    {
        WgContent wg = content.getBoundingBox();
        
        m_location.x = Integer.parseInt(wg.get(XmlConstants.shapeContentX));
        m_location.y = Integer.parseInt(wg.get(XmlConstants.shapeContentY));
        m_size.x = Integer.parseInt(wg.get(XmlConstants.shapeContentSizeX));
        m_size.y = Integer.parseInt(wg.get(XmlConstants.shapeContentSizeY));

        ArrayList<WgContent> elementsList = content.getElementsList();

        for(WgContent wgElt : elementsList)
        {
        	for(Primitive element : m_composingElements)
        	{
	            if(wgElt.get(XmlConstants.primitiveId).equals(String.valueOf(element.getElementId())))
	            {
	            	element.fromWgContent(wgElt);
	            }
        	}
        }
        setShape();
    }
    
    public int getLocationX() {
            return m_location.x;
    }

    public int getLocationY() {
            return m_location.y;
    }

    public void setLocationX(int x) {
            m_location.x = x;
    }

    public void setLocationY(int y) {
            m_location.y = y;
    }

    public int getSizeX() {
            return m_size.x;
    }

    public void setSizeX(int x) {
            this.m_size.x = x;
    }

    public int getSizeY() {
            return m_size.y;
    }

    public void setSizeY(int y) {
            this.m_size.y = y;
    }
}
